package cn.kgc.tangcco.controller;

import cn.kgc.tangcco.model.PageParam;
import cn.kgc.tangcco.model.ResponseText;
import cn.kgc.tangcco.model.ResultCode;
import cn.kgc.tangcco.pojo.Clazz;
import cn.kgc.tangcco.pojo.Student;
import cn.kgc.tangcco.service.ClazzService;
import cn.kgc.tangcco.service.StudentService;
import cn.kgc.tangcco.utils.reflect.BaseReflect;
import cn.kgc.tangcco.vo.StudentVo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/12/27 9:03
 */
@Controller
@RequestMapping
@SessionAttributes(value = {"clazzResponseText"})
@Slf4j
public class DefaultHandler {
    @Autowired
    private ClazzService clazzService;

    @Autowired
    private StudentService studentService;
    @GetMapping(value = "/index")
    public String thymeleaf(Model model){
        model.addAttribute("name","李昊哲");
        return "index";
    }
    @RequestMapping(value = "/data")
    public String data(Model model, Student student,
                       @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) throws Exception {
        ResponseText<List<Clazz>> clazzResponseText = new ResponseText<>(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), null);
        Clazz clazz = new Clazz();
        if (student.getCid() == null) {
            clazzResponseText = clazzService.queryClazzes(clazz);
            Integer cid = clazzResponseText.getData().get(0).getId();
            // 存储默认被点击的班级ID
            model.addAttribute("cid", cid);
            student.setCid(cid);
        } else {
            // 存储当前被点击的班级ID
            model.addAttribute("cid", student.getCid());
            clazzResponseText = clazzService.queryClazzes(clazz);
        }
        // 存储班级列表
        model.addAttribute("clazzResponseText", clazzResponseText);

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("pageParam", new PageParam(page, limit));
        paramMap.put("student", student);
        // PageInfo<StudentVo> pageInfo = studentService.queryVo(paramMap);
        Page<Student> pageInfo = studentService.query(paramMap);
        List<Student> students = pageInfo.getRecords();
        List<StudentVo> studentvos = new ArrayList<>();
        for (int i = 0; i < students.size(); i++) {
            StudentVo newStudent = (StudentVo) BaseReflect.fatherToChildWithFiled(students.get(i), new StudentVo());
            newStudent.convertIdCard();
            studentvos.add(newStudent);
        }
        Page studentVoPage = pageInfo;
        studentVoPage.setRecords(studentvos);
        // System.out.println(JSON.toJSONString(pageInfo));
        model.addAttribute("pageInfo", studentVoPage);
        return "data";
    }
}
